package com.tianchi.lexiang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.bean.Singer;
import com.tianchi.lexiang.util.AsyncBitmapLoader;

public class SingerDisplayAdapter extends BaseAdapter {
	private static final String TAG = "SingerDisplayAdapter";
	private LayoutInflater mInflater;

	private List<Singer> singers;
	private AsyncBitmapLoader asyncLoader = null;  
	
	public SingerDisplayAdapter(Context mContext, List<Singer> singers) {
		mInflater = LayoutInflater.from(mContext);
		this.singers = singers;
		this.asyncLoader = new AsyncBitmapLoader(mContext);
	}

	@Override
	public int getCount() {
		return singers.size();
	}

	@Override
	public Object getItem(int arg0) {
		return singers.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void changeDataSource(List<Singer> list){
		this.singers = list;
		notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag();
		} else {
			convertView = mInflater.inflate(R.xml.grid_item, null);
			holder = new ViewHolder();
			holder.photo = (ImageView) convertView
					.findViewById(R.id.singer_photo);
			holder.name = (TextView) convertView.findViewById(R.id.singer_name);
			convertView.setTag(holder);
		}

		final Singer mSinger = singers.get(position);
		Log.d(TAG, "will call asyncLoader.loadBitmap,mSinger.photoPath=>"+mSinger.photoPath);
		asyncLoader.loadBitmap(holder.photo, mSinger.photoPath, new AsyncBitmapLoader.ImageCallBack() {
			@Override
			public void imageLoad(ImageView imageView, Drawable drawable) {
				imageView.setImageDrawable(drawable);
			}
		});
		holder.name.setText(mSinger.name);
		return convertView;
	}

	private class ViewHolder {
		TextView name;
		ImageView photo;
	}
}
